﻿/*===文件上传Start===*/
var FileUpload = function (selector) {
    this.selector = selector;
};
FileUpload.prototype = {
    //宽度
    width: 85,
    height: 24,
    //是否自动上传，默认true
    auto: true,
    //Flash路径
    swf: '/Content/uploadify/uploadify.swf',
    //上传地址
    url: "",
    //上传按钮名称
    buttonText: "选择文件",
    //上传按钮图片
    buttonImage: null,
    //上传按钮样式
    buttonClass: '',
    //是否多文件上传，默认单文件
    multi: false,
    //附带表单数据，如{ "UpLoadSet": $('#file_upload').attr("data-options") }
    formData: {},
    //提交到服务端的Name名称
    fileObjName: "FileData",
    //上传文件后缀
    fileTypeExts: '*.*',
    //上传文件大小，默认不限制
    fileSizeLimit: 0,
    //上传文件数量
    uploadLimit: 0,
    //上传进度自定义模板
    itemTemplate: "",
    //上传成功事件
    uploadSuccess: function (objFile, objInvokeResult, objTarget) { },
    //上传完成事件
    uploaded: function () {
        return true;
    },
    //初始化配置
    uploadify: function (setts) {
        setts = setts || {};
        var $this = this;
        var defaultSetts = {
            width: $this.width,
            height: $this.height,
            auto: $this.auto,
            swf: $this.swf,
            uploader: $this.url,
            buttonText: $this.buttonText,
            buttonImage: $this.buttonImage,
            buttonClass: $this.buttonClass,
            multi: $this.multi,
            formData: $this.formData,
            fileObjName: $this.fileObjName,
            fileTypeExts: $this.fileTypeExts,
            fileSizeLimit: $this.fileSizeLimit,
            uploadLimit: $this.uploadLimit,
            //itemTemplate: '<div class="uploadifive-queue-item"><span class="filename"></span> | <span class="fileinfo"></span><div class="close"></div></div>',
            itemTemplate: $this.itemTemplate,
            onUploadSuccess: function (objFile, objInvokeResult) {
                try {
                    objInvokeResult = StringToJson(objInvokeResult);
                    $this.uploadSuccess(objFile, objInvokeResult, $this);
                    $this.uploaded();
                } catch (e) {
                    ShowMessage(objInvokeResult);
                }
            }
        };
        $.extend(true, defaultSetts, setts);
        //setTimeout(function () {
        //    $(this.selector).uploadify(defaultSetts);
        //}, 10);
        setTimeout(function () { }, 10);
        $(this.selector).uploadify(defaultSetts);
    }
};
/*===文件上传End===*/

//单图片上传
function SingleImgSuccess(objFile, objInvokeResult, objTarget) {
    if (objInvokeResult.ResultCode == "Success") {
        var objUpLoadSet;
        try {
            objUpLoadSet = JSON.parse(objTarget.formData.UpLoadSet);
        } catch (e) {
            objUpLoadSet = eval("(" + objTarget.formData.UpLoadSet + ")");
        }

        var imgSelector = "img." + objTarget.selector.replace("#", "");
        $(imgSelector).attr("src", objInvokeResult.Data);
        $(imgSelector).attr("title", objFile.name);
        $(imgSelector).tooltip({ content: '<span>' + objFile.name + '</span>', });

        $("input[name='" + objUpLoadSet.Name + "']").val(objInvokeResult.Data);

    } else {
        //alert(objInvokeResult.ResultMessage);
        ShowMessage(objInvokeResult.ResultMessage);
    }
}

//多图片上传
function MultiImgsSuccess(objFile, objInvokeResult, objTarget) {
    if (objInvokeResult.ResultCode == "Success") {
        var objUpLoadSet;
        try {
            objUpLoadSet = JSON.parse(objTarget.formData.UpLoadSet);
        } catch (e) {
            objUpLoadSet = eval("(" + objTarget.formData.UpLoadSet + ")");
        }
        var $hidval = $("input[name='" + objUpLoadSet.Name + "']");
        var $preview = $hidval.siblings(".upload_preview");

        var $multiimage = $(MultiImage(objFile.name, objInvokeResult.Data));
        $multiimage.find(".upload_image").tooltip({ content: '<span>' + objFile.name + '</span>', });
        $preview.append($multiimage);
        var objImageArray = [];
        $.each($preview.find(".upload_image"), function () {
            objImageArray.push($(this).attr("src"));
        });

        if (objImageArray.length > 0) {
            $hidval.val(objImageArray.join("$"));
            $preview.removeClass("upload_no_preview");
        } else {
            $hidval.val("");
            $preview.addClass("upload_no_preview");
        }

    } else {
        //alert(objInvokeResult.ResultMessage);
        ShowMessage(objInvokeResult.ResultMessage);
    }
}

function ImgOver(target) {
    $(target).find(".file_bar").addClass("file_hover");
    //$(target).closest(".file_bar").addClass("file_hover");
}
function ImgOut(target) {
    $(target).find(".file_bar").removeClass("file_hover");
    //$(target).closest(".file_bar").addClass("file_hover");
}
//删除图片事件
function ImgRemove(target) {
    var $preview = $(target).closest(".upload_preview");
    $(target).closest(".upload_append_list").remove();
    var objImageArray = [];
    var $hidval = $preview.siblings("input[type=hidden]");;
    $.each($preview.find(".upload_image"), function () {
        objImageArray.push($(this).attr("src"));
    });

    if (objImageArray.length > 0) {
        $hidval.val(objImageArray.join("$"));
        $preview.removeClass("upload_no_preview");
    } else {
        $hidval.val("");
        $preview.addClass("upload_no_preview");
    }
}
function MultiImage(ImageTitle, ImageSrc) {
    var objIamge = "<div class=\"upload_append_list\" onmouseover=\"ImgOver(this);\" onmouseout=\"ImgOut(this);\">"
                    + " <div class=\"file_bar\">"
                    + "     <div>"
                    + "         <span class=\"file_del\" data-index=\"1\" title=\"删除\" onclick=\"ImgRemove(this);\"></span>"
                    + "     </div>"
                    + " </div>"
                    + " <a class=\"imgBox\">"
                    + "     <div class=\"uploadImg\">"
                    + "         <img title=\"" + ImageTitle + "\" class=\"easyui-tooltip upload_image\" data-options=\"deltaY:-20\" src=\"" + ImageSrc + "\" onclick=\"OpenImagePreview(this);\">"
                    + "     </div>"
                    + " </a>"
                + "</div>";
    return objIamge;
}